﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Configuration;
using Insifo.BE.Gen;

namespace Insifo.DL.Gen
{
    public class DLAfp : DLTransaction, IDLTransaccion<BEAfp>
    {
        #region  Members

        /// <summary>
        /// Método obtiene las Afp
        /// </summary>
        /// <returns>Devuelve un DataSet</returns>
        public DataSet Listar_Sel(BEAfp pbe)
        {
            DataSet ds = new DataSet();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Afp_Listar_Sel");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                ds = sqlDB.ExecuteDataSet(dbCmd);
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {                
                //if (dbCmd.Connection.State == ConnectionState.Open)
                    //dbCmd.Connection.Close();
                dbCmd.Dispose();
            }
            return ds;
        }

        /// <summary>
        /// Método obtiene una Afp
        /// </summary>
        /// <returns>Devuelve un objeto</returns>
        public BEAfp Obtener(BEAfp pbe)
        {
            BEAfp be = new BEAfp();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Afp_Obtener");
            dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
            try
            {
                sqlDB.AddInParameter(dbCmd, "@ai_IdAfp", DbType.String, pbe.IdAfp);

                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdAfp = DBValue.Get<int>(reader, "IdAfp");
                        be.CodigoAfp = DBValue.Get<String>(reader, "CodigoAfp");
                        be.Descripcion = DBValue.Get<String>(reader, "Descripcion");
                        be.Periodo = DBValue.Get<String>(reader, "Periodo");
                        be.Fondo = DBValue.Get<Decimal>(reader, "Fondo");
                        be.Variable = DBValue.Get<Decimal>(reader, "Variable");
                        be.Seguro = DBValue.Get<Decimal>(reader, "Seguro");
                        be.Estado = DBValue.Get<String>(reader, "Estado");
                        be.UsuarioModificacion = DBValue.Get<String>(reader, "UsuarioModificacion");
                        be.FechaModificacion = DBValue.Get<DateTime>(reader, "FechaModificacion");
                        be.UsuarioCreacion = DBValue.Get<String>(reader, "UsuarioCreacion");
                        be.FechaCreacion = DBValue.Get<DateTime>(reader, "FechaCreacion");

                        break;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //if (dbCmd.Connection.State == ConnectionState.Open)
                //dbCmd.Connection.Close();
                dbCmd.Dispose();
            }
            return be;
        }

        #endregion  Members

        public BEAfp Insertar(BEAfp pEntidad)
        {
            throw new NotImplementedException();
        }
    }
}
